/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2019
 */

package net.dreamlu.db.manager;

import java.util.List;
import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import net.dreamlu.db.po.*;
import net.dreamlu.db.qo.*;
import net.dreamlu.db.repo.*;
/**
 *
 * @author lambert.wang email:lambert.wang(a)crediteyes.com
 * @version 1.0
 * @since 1.0
 */
@Service
@Transactional
public class RoleManager {

	@Autowired
	private RoleRepository roleRepository;

	public List<Role> queryAll() {
		return roleRepository.findAll();
	}

	/**
	 * 分页查询所有Role对象
	 * 
	 * @param pageable pageable  Pageable pageable = PageRequest.of(page, size,sort);
	 * @return
	 */
	public Page<Role> queryRoleByPage(Pageable pageable) {
		return roleRepository.findAll(pageable);
	}

	/**
	 * 根据条件分页查询Role对象
	 * 
	 * @param pageable
	 * @param query
	 * @return
	 */
	public Page<Role> queryRoleByPage(Pageable pageable, RoleQuery roleQuery) {
		Specification<Role> example = roleQuery.createSpecification();
		return roleRepository.findAll(example, pageable);
	}

	/**
	 * 不分页查询所有Role对象 不分页不带排序
	 * 
	 * @param query
	 * @return
	 */
	public List<Role> queryRole(RoleQuery roleQuery) {
		Specification<Role> example = roleQuery.createSpecification();
		return roleRepository.findAll(example);
	}

	/**
	 * 不分页查询单个Role对象 不分页不带排序
	 * 
	 * @param query
	 * @return
	 */
	public Role queryUniqueRole(RoleQuery roleQuery) {
		Specification<Role> example = roleQuery.createSpecification();
		Optional<Role> op = roleRepository.findOne(example);
		if (op.isPresent()) {
			return op.get();
		}
		return null;
	}

	/**
	 * 根据ID查询Role对象
	 * 
	 * @param id
	 * @return
	 */
	public Role queryRoleById(java.lang.Integer id) {
		Optional<Role> op = roleRepository.findById(id);
		if (op.isPresent()) {
			return op.get();
		}
		return null;
	}

	/**
	 * 保存Role对象
	 * 
	 * @param entity
	 */
	public void saveRole(Role role) {
		roleRepository.save(role);
	}

	/**
	 * 保存多个Role对象
	 * 
	 * @param entitys
	 */
	public void saveRole(List<Role> roles) {
		roleRepository.saveAll(roles);
	}

	/**
	 * 批量删除
	 * 
	 * @param deleteList
	 */
	public void deleteRole(List<Role> deleteList) {
		roleRepository.deleteAll(deleteList);
	}

	/**
	 * 批量删除根据查询条件
	 * 
	 * @param query
	 */
	public void deleteRole(RoleQuery roleQuery) {
		roleRepository.deleteInBatch(queryRole(roleQuery));
	}

	/**
	 * 批量删除根据id
	 * 
	 * @param ids
	 */
	public void deleteRoleByIds(java.lang.Integer... ids) {
		for (java.lang.Integer id : ids) {
			roleRepository.deleteById(id);
		}
	}

	/**
	 * 删除单个对象
	 * 
	 * @param entity
	 */
	public void deleteRole(Role role) {
		roleRepository.delete(role);
	}
}
